/**
 * Project name : sklay-core
 * File name : AbstractSqlScriptExecutor.java
 * Package name : com.sklay.core.database
 * Date : 2013-11-27
 * Copyright : 2013 , sklay.COM All Rights Reserved
 * Author : 1988fuyu@163.com
 */
package com.hoperun.framework.database;

import javax.sql.DataSource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;

/**
 * .
 * <p/>
 * 
 * @author <a href="mailto:1988fuyu@163.com">fuyu</a>
 * @version V1.0, 2012-12-8
 */
public abstract class AbstractSqlScriptExecutor implements InitializingBean
{
    
    private ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    
    private Resource[] scripts;
    
    private DataSource dataSource;
    
    private String charset = "UTF-8";
    
    public void setScripts(Resource[] scripts)
    {
        this.scripts = scripts;
    }
    
    public void setDataSource(DataSource dataSource)
    {
        this.dataSource = dataSource;
    }
    
    public void setCharset(String charset)
    {
        this.charset = charset;
    }
    
    public abstract boolean canExecute();
    
    @Override
    public void afterPropertiesSet()
        throws Exception
    {
        if (canExecute())
        {
            populator.setSqlScriptEncoding(charset);
            populator.setIgnoreFailedDrops(false);
            populator.setContinueOnError(false);
            for (Resource script : scripts)
            {
                populator.addScript(script);
            }
            DatabasePopulatorUtils.execute(populator, dataSource);
        }
    }
}